﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using skags.Models;

namespace skags.Controllers
{
    [HandleError]
    [LogError]
    public class PollController : Controller
    {
        DBDataContext _dc = new DBDataContext();

        readonly PollRepository pollRep = new PollRepository();
        
        /* GET: Skilar okkur seinustu könnun */
        public ActionResult Latest()
        {
            var result = pollRep.GetLatestPoll();
            return View(result);
        }

        /* GET: Kjósa í könnun */
        public ActionResult Vote()
        {
            var result = pollRep.GetNewestPoll();
            return View(result);
        }

        
        /* POST: Fall sem tekur við atkvæði í könnunum */
        [HttpPost]
        public ActionResult Vote(FormCollection formData)
        {
            if (Response.Cookies["Brjostagjof_" + formData["id"]] != null)
            {
                var val = formData["Opt"];
                pollRep.UpdateVote(val);

                Response.Cookies["Brjostagjof_" + formData["id"]].Value = "true";
                Response.Cookies["Brjostagjof_" + formData["id"]].Expires = DateTime.Now.AddMonths(1);

                return RedirectToAction("../Home/Index");
            }
            else
            {
                return View();
            }
        }

        /* GET: Fall sem sýnir allar kannanir í kerfinu */
        public ActionResult Polls()
        {
            var result = pollRep.GetAllPolls();
            return View(result);
        }
    }
}
